﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharpDom;
using System.Web.Mvc.Html;
using Bzure.Apps.Security.ViewModels;

namespace Bzure.Apps.Security.Views
{
    public class AuthorizeUser : PageView<AuthorizeUserViewModel>
    {
        public virtual ContainerTag TitleContent(ContainerTag container)
        {
            return container[Model.Title];
        }

        string Script2(string url)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("var url =\"{0}\";", url);
            sb.Append(" var Authorize = function () {");
            sb.Append(" var form = $(\"#AuthorizeUser_Form\");");
            sb.Append("var serializedForm = form.serialize(); ");
            sb.Append(" $.post(url, serializedForm, function (result) {");
            sb.Append(" $(\"#content_box_messages\").html(result);        ");
            sb.Append(" $(\".close\").click(");
            sb.Append("function () {");
            sb.Append("   $(this).fadeTo(400, 0, function () {");
            sb.Append("  $(this).slideUp(400);");
            sb.Append("  });   return false;	}	);");
            sb.Append(" }); };");
            return sb.ToString();
        }
        public virtual ContainerTag MainContent(ContainerTag container)
        {
            return container
            [
                script.attr(type: MimeType.text.javascript, src: this.Url.RouteUrl("ResourceAccess_Script", new { location = "SecurityApp", id = "jquery-1.7.2.min.js" })),
                script.attr(type: MimeType.text.javascript)[Script2(Model.Url)],
                h2[Model.Title],
                p.attr(id: "page-intro")["配置用户的状态和用户所具有的安全角色。"],
                div.attr(@class: "clear"),
                div.attr(@class: "content-box")
                [
                     div.attr(@class: "content-box-header")
                      [
                            h3["用户授权"],
                            ul.attr(@class: "content-box-tabs")
                            [
                               li[a.attr(href: "#tab1", @class: "default-tab")["配置"]]
                            ],
                            div.attr(@class: "clear")
                      ],
                     div.attr(@class: "content-box-content")
                      [
                          div.attr(@class: "tab-content default-tab", id: "tab1")
                          [
                                div.attr(id: "content_box_messages"),
                                form.attr(id: "AuthorizeUser_Form")
                                [
                                       p.attr(id: "AuthorizeUser_UserName")
                                        [
                                             label["用&nbsp;户&nbsp;名：&nbsp;" + Model.UserName]
                                        ],
                                        p.attr(id: "AuthorizeUser_State")
                                        [
                                             label.attr(@for: "UserState")["用户激活："],
                                             input.attr(type: InputType.checkbox, id: "UserState", name: "UserState", @checked: Model.UserState ? (Checked?)Checked.@checked : null)
                                        ],
                                         p.attr(id: "AuthorizeUser_RoleList")
                                        [
                                             label.attr(id: "AuthorizeUser_Roles_Title")["用户角色："],
                                              ForEach(Model.Roles,(item,c)=>c[
                                                      label.attr(@class: "authorizeUserRole label_check", id: "CK_"+item.ID ,@for :"Role_"+item .ID)
                                                      [
                                                       input.attr(type: InputType.checkbox, id: "Role_" + item.ID, name: "Role_" + item.ID,value :item.ID , @checked: Model.RIDs.Contains (item .ID ) ? (Checked?)Checked.@checked : null),
                                                       item.Name 
                                                       ]
                                                  ])
                                        ],
                                        p.attr(id: "AuthorizeUser_Submit")
                                        [
                                            input.attr(type: InputType.button, @class: "button", value: "保存", onclick: "Authorize();")
                                        ],
                                         p.attr(id: "AuthorizeUser_ReturnUrl")
                                        [
                                             a.attr(href: Url.RouteUrl("SecurityApp_User", new { action = "UserList" }))["返回用户列表"]
                                        ]
                                ]
                          ]
                      ]
                ]
            ];
        }

        public override ContainerTag CreateTagTree(ContainerTag container)
        {
            return ReusableCreateTagTree(TitleContent, MainContent, Model.MasterViewModel);
        }
    }
}
